package com.ny.zmb.openapi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ny.zmb.openapi.constant.PHPConstant;
import com.ny.zmb.openapi.controller.entity.Result;
import com.ny.zmb.openapi.entity.PmOrderRefundLog;
import com.ny.zmb.openapi.mapper.PmOrderRefundLogMapper;
import com.ny.zmb.openapi.service.PmOrderRefundLogService;
import com.ny.zmb.openapi.ysutil.YsHttpUtil;
import com.ny.zmb.openapi.ysutil.common.ServiceEnum;
import com.ny.zmb.openapi.ysutil.from.BizReqJsonCollege.BizReqJson;
import com.ny.zmb.openapi.ysutil.from.BizReqJsonCollege.BizReqJsonRefund;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 退款订单
 *
 * @author ChenZhiZheng
 * @date 20241107
 */
import static com.ny.zmb.openapi.constant.PHPConstant.*;

@Service

public class PmOrderRefundLogServiceImpl extends ServiceImpl<PmOrderRefundLogMapper, PmOrderRefundLog> implements PmOrderRefundLogService {

    @Resource
    private YsHttpUtil ysHttpUtil;

    @Resource
    private PmOrderRefundLogMapper pmOrderRefundLogMapper;
    private QueryWrapper<PmOrderRefundLog> queryWrapper = new QueryWrapper<>();

    @Override
    public PmOrderRefundLog getByOrigRequestNo(String origRequestNo) {
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orig_request_no", origRequestNo);
        return pmOrderRefundLogMapper.selectOne(queryWrapper);
    }

    @Override
    public PmOrderRefundLog getByRequestNo(String requestNo) {
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("request_no", requestNo);
        return pmOrderRefundLogMapper.selectOne(queryWrapper);
    }

    /**
     * 退款请求
     *
     * @param srcMerchantNo
     * @param obj
     * @return
     */
    @Override
    public Result refund(String srcMerchantNo, BizReqJsonRefund obj) {
        // 保存
        PmOrderRefundLog pmOrderRefundLog = new PmOrderRefundLog(obj);
        save(pmOrderRefundLog);
        // 处理回调
        if (obj.getNotifyUrl() != null && obj.getNotifyUrl().isEmpty()) {
            obj.setNotifyUrl(JAVA_BASE_URL + "/notify/refundNotify");
        }
        JSONObject result = doActive(ServiceEnum.WALLET_REFUND, ServiceEnum.WALLET_REFUND.getApiName(), srcMerchantNo, obj);
        updateById(pmOrderRefundLog.update(result.getString("state"), result.getString("subMsg")));
        return Result.ok(result);
    }

    /**
     * 查询请求参数
     *
     * @param srcMerchantNo
     * @param obj           {"requestNo"}  交易退款请求号(交易退款请求的业务请求号);
     * @return
     */
    @Override
    public Result queryTradeRefund(String srcMerchantNo, BizReqJsonRefund obj) {
        JSONObject result;
        PmOrderRefundLog pmOrderRefundLog;
        // 查询
        result = doActive(ServiceEnum.QUERY_TRADE_REFUND_ORDER, "queryTradeRefund", srcMerchantNo, obj);
        pmOrderRefundLog = getByRequestNo(obj.getRequestNo());
        if (pmOrderRefundLog != null) {
            // 找到记录,更新
            updateById(pmOrderRefundLog.update(result.getString("state"), result.getString("subMsg")));
        }
        return Result.ok(result);
    }

    private JSONObject doActive(ServiceEnum anyEnum, String apiName, String srcMerchantNo, Object obj) {

        try {
            return ysHttpUtil.sendBase(anyEnum, null, null, null, apiName, srcMerchantNo, obj);
        } catch (Exception var) {
            throw new RuntimeException(var.getMessage());
        }
    }
}
